/**
 * 建筑类型管理API接口
 * @author Wu.Liang
 * @date 2025-01-30
 */

import api from './request'
import type { ApiResponse } from './request'
import { useUserStore } from '@/store/user'

// 基础建筑类型分类接口
export interface BuildingTypeCategory {
  id: number
  parentId: number
  categoryName: string
  categoryCode: string
  categoryType: 'DIVISION' | 'TYPE'
  divisionType?: string
  description?: string
  sortOrder: number
  status: number
  createTime: string
  updateTime: string
}

// 物业公司建筑类型接口
export interface PropertyCompanyBuildingType {
  id: number
  propertyCompanyId: number
  propertyCompanyName?: string
  buildingTypeName: string
  buildingTypeCode: string
  categoryIds: number[]
  categoryNames?: string[]
  description: string
  sortOrder: number
  status: number
  createTime: string
  updateTime: string
}

// 创建建筑类型DTO
export interface CreateBuildingTypeDTO {
  id?: number
  propertyCompanyId?: number
  buildingTypeName: string
  buildingTypeCode: string
  categoryIds: number[]
  description?: string
  sortOrder?: number
  status: number
  remark?: string
}

// 更新建筑类型DTO
export interface UpdateBuildingTypeDTO {
  id: number
  buildingTypeName?: string
  buildingTypeCode?: string
  categoryIds?: number[]
  description?: string
  sortOrder?: number
  status?: number
  remark?: string
}

// 查询参数接口
export interface BuildingTypeQueryParams {
  buildingTypeName?: string
  buildingTypeCode?: string
  status?: number
  current?: number
  size?: number
}

// 分页响应接口
export interface BuildingTypePageResponse {
  records: PropertyCompanyBuildingType[]
  total: number
  size: number
  current: number
  pages: number
}

/**
 * 建筑类型管理API
 */
export const buildingTypeApi = {
  // ==================== 基础分类管理 ====================
  
  /**
   * 获取划分方式列表
   */
  getDivisionList: () =>
    api.get<ApiResponse<BuildingTypeCategory[]>>('/api/region/building-type/category/division'),

  /**
   * 根据划分方式获取类型列表
   */
  getTypeListByDivision: (divisionId: number) =>
    api.get<ApiResponse<BuildingTypeCategory[]>>(`/api/region/building-type/category/type/${divisionId}`),

  /**
   * 根据ID列表获取类型详情
   */
  getTypeListByIds: (ids: number[]) =>
    api.post<ApiResponse<BuildingTypeCategory[]>>('/api/region/building-type/category/ids', ids),

  // ==================== 物业公司建筑类型管理 ====================
  
  /**
   * 创建建筑类型
   */
  createBuildingType: (data: CreateBuildingTypeDTO) =>
    api.post<ApiResponse<void>>('/api/region/building-type/property-company', data),

  /**
   * 批量创建建筑类型
   */
  batchCreateBuildingType: (data: CreateBuildingTypeDTO[]) =>
    api.post<ApiResponse<void>>('/api/region/building-type/property-company/batch', data),

  /**
   * 获取物业公司建筑类型列表
   */
  getBuildingTypeList: (propertyCompanyId: number, params?: BuildingTypeQueryParams) =>
    api.get<ApiResponse<BuildingTypePageResponse>>(`/api/region/building-type/property-company/${propertyCompanyId}`, { params }),

  /**
   * 更新建筑类型
   */
  updateBuildingType: (data: UpdateBuildingTypeDTO) =>
    api.put<ApiResponse<void>>('/api/region/building-type/property-company', data),

  /**
   * 删除建筑类型
   */
  deleteBuildingType: (id: number) =>
    api.delete<ApiResponse<void>>(`/api/region/building-type/property-company/${id}`),

  /**
   * 获取建筑类型详情
   */
  getBuildingTypeDetail: (id: number) =>
    api.get<ApiResponse<PropertyCompanyBuildingType>>(`/api/region/building-type/property-company/detail/${id}`),

  /**
   * 根据物业公司ID获取所有建筑类型（不分页）
   */
  getBuildingTypesByPropertyCompanyId: (propertyCompanyId: number) =>
    api.get<ApiResponse<PropertyCompanyBuildingType[]>>('/api/region/building-type/public/list'),

  /**
   * 获取平台公共建筑类型列表（分页）
   */
  getPublicBuildingTypeList: (params?: BuildingTypeQueryParams) =>
    api.get<ApiResponse<BuildingTypePageResponse>>('/api/region/building-type/public', { params }),

  /**
   * 获取平台公共建筑类型列表（不分页）
   */
  getPublicBuildingTypes: () =>
    api.get<ApiResponse<PropertyCompanyBuildingType[]>>('/api/region/building-type/public/list'),

  /**
   * 批量删除建筑类型
   */
  batchDeleteBuildingType: (ids: number[]) =>
    api.delete<ApiResponse<void>>('/api/region/building-type/property-company/batch', { data: { ids } }),

  /**
   * 批量更新建筑类型状态
   */
  batchUpdateStatus: (ids: number[], status: number) =>
    api.put<ApiResponse<void>>('/api/region/building-type/property-company/batch/status', { ids, status }),

  // ==================== 物业公司相关 ====================
  
  /**
   * 获取物业公司列表（用于选择）
   */
  getPropertyCompanyList: () =>
    api.get<ApiResponse<any[]>>('/api/property/company/list'),

  /**
   * 根据用户权限获取物业公司列表
   */
  getUserPropertyCompanies: (userId?: number) => {
    // 如果没有传入userId，需要从store获取当前用户ID
    const userStore = useUserStore()
    const currentUserId = userId || userStore.userInfo?.id
    if (!currentUserId) {
      return Promise.reject(new Error('用户ID不能为空'))
    }
    return api.get<ApiResponse<any[]>>(`/api/user/property-companies/user/${currentUserId}`)
  },

  // ==================== 导出功能 ====================
  
  /**
   * 导出建筑类型列表
   */
  exportBuildingTypeList: (propertyCompanyId: number, params?: BuildingTypeQueryParams) =>
    api.get('/api/region/building-type/property-company/export', { 
      params: { property_company_id: propertyCompanyId, ...params },
      responseType: 'blob'
    }),

  // ==================== 统计分析 ====================
  
  /**
   * 获取建筑类型统计信息
   */
  getBuildingTypeStatistics: (propertyCompanyId?: number) =>
    api.get<ApiResponse<any>>('/api/region/building-type/statistics', { 
      params: { property_company_id: propertyCompanyId } 
    })
}

export default buildingTypeApi
